#include "ZombieSpawner.h"
#include "ZombieManager.h"

// Constructores y destructores ----------------------------------------

ZombieSpawner::ZombieSpawner(OpenSteer::Vec3 position,
                             ZombieManager* pZombieManager,
                             Human* pHuman,
                             float spawnTime, int maxZombies) :
  _spawnerPosition(position), _pZombieManager(pZombieManager),
  _pHuman(pHuman), _spawnTime(spawnTime), _maxZombies(maxZombies) {

  _nZombies = 0;
  _spawnCounter = _spawnTime;
}

ZombieSpawner::~ZombieSpawner() {}

// Métodos propios -----------------------------------------------------

void
ZombieSpawner::update(const float currentTime, const float elapsedTime) {

  // Decrementar contador de generación
  _spawnCounter -= elapsedTime;

  // Si transcurrió el tiempo de generación
  if (_spawnCounter <= 0) {

    // Crear zombi
    spawnZombie();

    // Reiniciar contador
    _spawnCounter = _spawnTime;
  }
}

void
ZombieSpawner::spawnZombie() {

  // Comprobar máximo de zombis
  if (_maxZombies > 0 &&
      _nZombies >= _maxZombies) return;

  // Calcular posición zombi
  OpenSteer::Vec3 zombiePosition =
    _spawnerPosition + OpenSteer::randomVectorOnUnitRadiusXZDisk();

  // Generar zombi
  _pZombieManager->createZombie(zombiePosition);

  // Incrementar contador de zombis
  _nZombies++;
}
